#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>

using namespace std;

int tickets = 1000;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void *start_routine(void* args)
{
    string name = static_cast<const char*> (args);
    while(true)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond,&mutex); // 为什么要有mutex, 后面说
        // 判断暂时省略
        cout << name << " ->" << tickets << endl;
        tickets--;
        pthread_mutex_unlock(&mutex);
    }
}

int main()
{
    // 通过条件变量控制线程的执行
    pthread_t t[5];
    for(int i = 0; i < 5 ; i++)
    {
        char *name = new char[64];
        snprintf(name,64,"thread %d", i+1);
        pthread_create(&t[i],nullptr,start_routine,name);
    }


    // pthread_t t1, t2;
    // pthread_create(&t1,nullptr,start_routine,(void*)"thread 1");
    // pthread_create(&t2,nullptr,start_routine,(void*)"thread 2");

    while(true)
    {
        sleep(1);
       // pthread_cond_signal(&cond);
        pthread_cond_broadcast(&cond);
        cout << "main thread wakeup one thread..." << endl;
    }

    for(int i = 0; i < 5; i++)
    {
        pthread_join(t[i],nullptr);
    }

    // pthread_join(t1,nullptr);
    // pthread_join(t2,nullptr);
    return 0;
}